package net.liuyimin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.liuyimin.entity.StockEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


/**
 * mapper 接口
 */
public interface StockMapper extends BaseMapper<StockEntity>  {

    /**
     * UPDATE tb_stock SET count = (count - 1)
     * WHERE product_code = "1001" and stock_code = "001" and count >= 1 ;
     */
    @Update("UPDATE tb_stock SET count = (count - #{count}) WHERE product_code = #{productCode} and count >= #{count}")
    int updateStock(@Param("productCode") String productCode, @Param("count") Integer count);

    @Select("SELECT * FROM tb_stock WHERE product_code = #{productCode} FOR UPDATE")
    List<StockEntity> selectStockForUpdate(String productCode);

}
